#!/bin/bash
#leao:Fri Aug 23 06:06:26 PM CST 2024 
  
function build() {  
    typeset -u capital  
    capital=$1  
    mkdir "$1" 
    mkdir "$1/Headers"
    mkdir "$1/Sources" 
    touch "$1/Headers/${1}.h" "$1/Sources/${1}.cpp" "$1/Sources/main.cpp" "$1/Makefile" "$1/Headers/template.h" "$1/Sources/template.cpp"
    echo "#ifndef __${capital}_H_  
#define __${capital}_H_  
#include <iostream> 
using namespace std;


#endif" > "$1/Headers/${1}.h"

  echo "#ifndef __TEMPLATE_H_  
#define __TEMPLATE_H_ 
#include \"${1}.h\" 


#endif" > "$1/Headers/template.h" 

    echo "#include \"../Headers/${1}.h\"   
  
" > "$1/Sources//${1}.cpp" 
 
    echo "#include \"../Headers/template.h\"   
  
" > "$1/Sources/template.cpp" 
 echo " 
#include \"../Headers/${1}.h\" 
#include \"template.cpp\" 
int main(int argc, char *argv[]){  
    
   
    return 0;  
}" > "$1/Sources/main.cpp"  
    echo "SRCS:=\$(wildcard Sources/*.cpp)
OBJS:= \$(patsubst Sources/%.cpp,Sources/%.o,\$(SRCS))
CC:= g++
main: \$(OBJS)
	\$(CC) \$^ -o \$@  -g 
%.o:%.cpp
	\$(CC) -c \$^ -o \$@ -g
clean:
	\$(RM) \$(OBJS) main
rebuild:clean main
" > $1/Makefile

}  
  
if [ -z "$1" ]; then  
    echo "error: missing argument"  
    exit 1  
fi  
  
if [ -d "$1" ]; then  
    echo "${1} 已经是一个目录, 是否重构(y/Y)以继续, 或退出程序(n/N)?"  
    read val  
    case $val in  
        [yY]*)  
            rm -rf "$1"  
            build "$1" ;;  
        [nN]*)  
            exit 0 ;;  
        *)  
            echo "无效输入，请输入 y/Y 或 n/N。"  
            exit 1 ;;  
    esac  
else  
    build "$1"  
fi
